package com.example.spring.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.example.spring.form.Contact;
import com.example.spring.model.Role;
import com.example.spring.model.User;
import com.example.spring.service.ContactService;
import com.example.spring.service.RoleService;
import com.example.spring.service.UserService;

@Controller
public class ContactController {

	@Autowired
	ContactService contactService;
	
	@Autowired
	UserService userService;
	
	@Autowired
	RoleService roleService;
	
	@RequestMapping(value = "/index.htm" , method = RequestMethod.GET)
	public String listContacts(ModelMap contact){
		contact.addAttribute("contact", new Contact());
		contact.addAttribute("contactList" , contactService.listContact());
		
		return "contact";
	}
	
	@RequestMapping(value = "/add.htm" , method = RequestMethod.POST)
	public String addContact(@ModelAttribute("contact") Contact contact , BindingResult bindingResult){
		contactService.addContact(contact);
		return "redirect:/index.htm";
	}
	
	@RequestMapping(value = "/delete/{contactId}")
	public String deleteContact(@PathVariable("contactId") Integer id){
		System.out.println("=========== delete ========== " + id);
		contactService.removeContact(id);
		return "redirect:/index.htm";
	}
}
